package cn.wbnull.springbootdemo.service;

import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;

import cn.wbnull.springbootdemo.dao.UserMapper;
import cn.wbnull.springbootdemo.model.Users;

/**
 * 功能描述
 *
 * @author EDZ
 * @since 2022-04-02
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    public List<Users> selectList() {
        List<Users> usersList = new ArrayList<>();
        usersList = userMapper.selectList();
        if (usersList == null) {
            return null;
        }
        return usersList;
    }

    public List<Users> select(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        List<Users> usersList = new ArrayList<>();
        redisTemplate.opsForValue().increment("too", 18);
        usersList = userMapper.select(name);
        if (usersList.size() == 0) {
            return new ArrayList<>();
        }
        return usersList;
    }

    public List<Users> selectlimit(Users users, int pagenum, int pageSize) {

        if (pagenum <= 0) {
            return new ArrayList<>();
        }
        int startIndex = (pagenum - 1) * pageSize;
        String name = users.getName();
        Integer id = users.getId();
        String status = users.getStatus();
        if (StringUtils.isEmpty(name)) {
            return new ArrayList<>();
        }
        if (id == null) {
            return new ArrayList<>();
        }
        if (StringUtils.isEmpty(status)) {
            return new ArrayList<>();
        }

        List<Users> usersList = userMapper.list(name, startIndex, pageSize);
        if (CollectionUtils.isEmpty(usersList)) {
            return new ArrayList<>();
        }
        return usersList;

    }

    /**
     * 表单导出
     *
     * @param type     表单数据
     * @param response 会话id
     * @throws IOException 会话id
     */
    public void derive(HttpServletResponse response) throws IOException {
// 表头样式
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();

        // 内容样式

        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();

        // 设置内容靠左对齐

        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);

        // 设置表头居中对齐
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String fileName = time + ".xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
        buildExcelData(excelWriter);

        // 千万别忘记关流,finish会帮忙关流
        excelWriter.finish();
    }

    //查询数据
    private void buildExcelData(ExcelWriter excelWriter) {
        // 查询到数据
//        List<InputImsBanner> bannerList = inputImsBannerDao.queryBanner();
        // 返回写入的数据
//        WriteSheet writeSheet = EasyExcel.writerSheet(0, "banner")
//                .head(Users.class)
//                .registerWriteHandler(new CustomCellWriteHandlerUtil())
//                .build();
//        excelWriter.write(bannerList, writeSheet);
    }
}
